﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebVideo.Areas.Admin.Models;
using CarlosAg.ExcelXmlWriter;
using System.IO;
namespace WebVideo.Controllers
{
    public class ReportController : Controller
    {
        //
        // GET: /Report/
       [HttpPost]
        public Workbook Index(Workbook book1, IList<Report> TieuDe, IList<Report> NoiDung, string Name)
        {
            Workbook book = book1;
            try
            {
                Worksheet sheet = book.Worksheets.Add("Report");
                for (int i = 0; i < 20; i++)
                {
                    WorksheetColumn col = new WorksheetColumn(150);
                    sheet.Table.Columns.Add(col);
                }

                for (int i = 0; i < (NoiDung.Count() /TieuDe.Count()) + 6; i++)
                {
                    WorksheetRow Row = new WorksheetRow();
                    sheet.Table.Rows.Add(Row);
                    for (int j = 0; j < 20; j++)
                    {
                        WorksheetCell Cell = new WorksheetCell();
                        sheet.Table.Rows[i].Cells.Add(Cell);
                    }
                }
                // Tạo định dạng font
                WorksheetStyle style = book.Styles.Add("TieuDe");
                style.Font.FontName = "Arial";
                style.Font.Size = 15;
                style.Interior.Color = "#009966";   // dong nay to background
                style.Interior.Pattern = StyleInteriorPattern.Solid; // dong nay to background
                style.Font.Bold = true;
                style.Alignment.Horizontal = StyleHorizontalAlignment.Center;
                style.Font.Color = "White";

                // phần thống kê
                // style
                WorksheetStyle style4 = book.Styles.Add("ThongKeTN");
                style4.Font.FontName = "Arial";
                style4.Interior.Color = "#FF9900";   // dong nay to background
                style4.Interior.Pattern = StyleInteriorPattern.Solid; // dong nay to background
                style4.Font.Bold = true;
                style4.Alignment.Horizontal = StyleHorizontalAlignment.Center;

                WorksheetStyle style5 = book.Styles.Add("ThongKeTNChiTiet");
                style5.Font.FontName = "Arial";
                style5.Interior.Color = "#99CCFF";   // dong nay to background
                style5.Interior.Pattern = StyleInteriorPattern.Solid; // dong nay to background

                WorksheetCell inputCell = new WorksheetCell();

                inputCell = new WorksheetCell();
                inputCell.MergeAcross = 3;
                inputCell.Data.Type = DataType.String;
                inputCell.Data.Text = Name.ToUpper();// Danh sách các user
                inputCell.StyleID = "TieuDe";
                sheet.Table.Rows[1].Cells[1] = inputCell;

                // tiêu đề 
                int cot = 0;
                foreach (var item in TieuDe)
                {
                    inputCell = new WorksheetCell();
                    inputCell.Data.Type = DataType.String;
                    inputCell.Data.Text = item.Name;// Danh sách các user
                    inputCell.StyleID = "ThongKeTNChiTiet";
                    sheet.Table.Rows[4].Cells[cot] = inputCell;
                    cot++;
                }

                // nội dung
                int dong = 5;
                 cot = 0;
                foreach (var item in NoiDung)
                {
                    inputCell = new WorksheetCell();
                    inputCell.Data.Type = DataType.String;
                    inputCell.Data.Text = item.Name;
                    sheet.Table.Rows[dong].Cells[cot] = inputCell;
                    cot++;
                    if (cot == TieuDe.Count())
                    {
                        dong++;
                        cot = 0;
                    }
                }
                return book;
            }
            catch
            {
                return book;
            }
        }

    }
}
